package Persistence;

import java.sql.ResultSet;
import java.sql.SQLException;

import Dominion.*;

public class ArticleDAO extends CrudDAO<Article>{

    @Override
    public Article create(Article articulo) {
        String sqlQuerry = "INSERT INTO Article (title, publishing_date, idAuthor, commentAuthor, state, mark) VALUES ('"
        		+articulo.getTitle()+"','"+articulo.getPublishing_date()+"','"+articulo.getId_author()+"','"
        		+articulo.getComment_author()+"','"+articulo.getState()
        		+"','"+articulo.getMark()+"')";
        try {
			AgenteBD.getDbCon().queryUpdate(sqlQuerry);
		} catch (SQLException e) {
			e.printStackTrace();
		}
        return null;
    }

    @Override
    public void delete(Article articulo) {
    	 try {
 			AgenteBD.getDbCon().queryUpdate("DELETE FROM Article WHERE idArticle='"+articulo.getId()+"'");
 		} catch (SQLException e) {
 			e.printStackTrace();
 		}
    }

    @Override
    public Article read(Article articulo) throws SQLException {
      ResultSet rs = AgenteBD.getDbCon().query(
                "SELECT * FROM Article WHERE idArticle=" + articulo.getId()+ "");
        if (rs != null) {
            try {
                while (rs.next()) {
                    articulo.setId(rs.getInt("idArticle"));
                    articulo.setTitle(rs.getString("title"));
                    articulo.setPublishing_date(rs.getString("publishing_date"));
                    articulo.setId_author(rs.getString("idAuthor"));
					articulo.setComment_author(rs.getString("commentAuthor"));
					articulo.setState(rs.getString("state"));
					articulo.setMark(rs.getInt("mark"));
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        rs.close();
        return articulo;
    }

    @Override
    public void update(Article articulo) {
    	 String sqlQuerry="UPDATE Article SET title='"+articulo.getTitle()
		         +"', publishing_date='" + articulo.getPublishing_date()
		         +"', idAuthor='" +articulo.getId_author()
		         +"', commentAuthor='"+articulo.getComment_author()
		         +"', state='"+ articulo.getState()
		         +"', mark='"+articulo.getMark()
		         + "' WHERE idArticle='" + articulo.getId()+ "'";
		try {
			AgenteBD.getDbCon().queryUpdate(sqlQuerry);
		} catch (SQLException e) {
			e.printStackTrace();
		}       
    }
}
